<?php

namespace App\Http\Controllers;

use App\Award;
use App\Http\Requests\AddAwardRequest;
use App\MakeTable\Table;
use App\Redict\Success;
use App\SoftImage\SoftImage;
use Illuminate\Http\Request;

class AwardController extends Controller
{
    public function __construct()
    {
        $this->middleware('can:root_Admin_award');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
  
    public function index()
    {
        $pagename = '奖品管理';
        $modelurl = 'awards';
        $tablecate = ['tablecate'=>'奖品'];
        $headname = ['奖品名称','奖品图片','积分','简介','添加日期','修改日期','操作'];
        $tablehead = ['name','award_image','integration','intro','created_at','updated_at','id'];
        $awardtable = new Table($tablehead,$page = 8);
        $model = $awardtable->viewdata(Award::class);
        $models = $awardtable->viewpagedata(Award::class);
        return view('admin.home.tableshow',compact('tablecate','model','tablehead','models','headname','pagename','modelurl'));


    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //新增奖品
        $modelurl = 'awards';
        $pagename = '新增奖品';
        $formhead = ['name','award_image','integration','intro'];
        $formname = ['奖品名称','奖品图片','积分','简介'];
        $formhead = collect($formname)->zip($formhead)->toArray();
        return view('admin.home.awardform',compact('pagename','tags','modelurl','formhead'));
        //表单，位置 resources/views/admin/home/awardform.blade
    }

    /**
     * Store a newly created resource i4n storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddAwardRequest $request)
    {
        //添加验证
        $data = array_merge(
            $request->all(),
            SoftImage::image($request->file('award_image'))//处理图片的函数
        );
        $award = Award::create($data);
        return redirect('admin/awards')->with('message', '成功添加'.$award->name);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $award = Award::findOrFail($id);
        $modelurl = 'awards';
        $pagename = $award->name;
        $formhead = ['name','award_image','integration','intro','updated_at'];
        $formname = ['奖品名称','奖品图片','积分','简介','修改日期'];
        $formhead = collect($formname)->zip($formhead)->toArray();

        return view('admin.home.editawards',compact('pagename','award','formhead','modelurl'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
public function update(Request $request, $id)
{
    //
    $data = array_merge(
        $request->all()
    );

    if($request->file('award_image')){
        $data = array_merge($data, SoftImage::image($request->file('award_image')));
    }

    $award = Award::findOrFail($id);
    $award->update($data);
    //
    return Success::back();
}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Award::destroy($id);
        return Success::back();
    }
}
